﻿using IService;
using Microsoft.AspNetCore.Mvc;

namespace XingWeiDaWMSProject.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class DictController : ControllerBase
    {
        private readonly ISysDictService _dictService;

        public DictController(ISysDictService dictService)
        {
            _dictService = dictService;
        }

        /// <summary>
        /// 根据“字典类型编码”获取字典数据列表
        /// 示例调用：GET api/Dict/datas/Sex → 返回性别字典（男、女等）
        /// </summary>
        [HttpGet("datas/{dictTypeCode}")]
        public async Task<IActionResult> GetDictDatas(string dictTypeCode)
        {
            var dictDatas = await _dictService.GetDictDatasByCodeAsync(dictTypeCode);
            return Ok(dictDatas);
        }

        /// <summary>
        /// 根据“字典类型编码 + 字典值”获取字典标签
        /// 示例调用：GET api/Dict/label?dictTypeCode=Sex&dictValue=1 → 返回“男”
        /// </summary>
        [HttpGet("label")]
        public async Task<IActionResult> GetDictLabel(string dictTypeCode, string dictValue)
        {
            var label = await _dictService.GetDictLabelAsync(dictTypeCode, dictValue);
            return Ok(label);
        }

        /// <summary>
        /// 刷新**所有字典**的 Redis 缓存（从数据库重新加载）
        /// 示例调用：POST api/Dict/refresh/all
        /// </summary>
        [HttpPost("refresh/all")]
        public async Task<IActionResult> RefreshAllDictCache()
        {
            await _dictService.RefreshAllDictCacheAsync();
            return Ok("所有字典缓存已刷新");
        }

        /// <summary>
        /// 刷新**单个字典类型**的 Redis 缓存
        /// 示例调用：POST api/Dict/refresh/Sex
        /// </summary>
        [HttpPost("refresh/{dictTypeCode}")]
        public async Task<IActionResult> RefreshDictCache(string dictTypeCode)
        {
            await _dictService.RefreshDictCacheByCodeAsync(dictTypeCode);
            return Ok($"字典[{dictTypeCode}]缓存已刷新");
        }


    }
}
